package com.main.ok;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
				DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);
		model.addAttribute("serverTime", formattedDate);
		return "home";
	}

	@RequestMapping(value = "/test",headers = "name",method=RequestMethod.GET)
	public String test(String name,HttpServletRequest request) {
		System.out.println(name);
		System.out.println(request.getHeader("name"));
		System.out.println(request.getHeader("age"));
		System.out.println(request.getCharacterEncoding());
		return "helloworld";

	}
	@RequestMapping(value="/testvoid")
	public void testvoid(HttpServletRequest request,HttpServletResponse response){
		try {
			response.setContentType("text/html;charset=utf8");
			response.getWriter().println("<font style='color:red'>hello</font>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**InputStream OutputStream 也可以作为参数
	 * 
	 * @param reader
	 * @param writer
	 */
	@RequestMapping("/testrw")
	public void testrw(Reader reader,Writer writer){
		try {
			writer.write("hello,ketty");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
